import { Permission, PermissionsAndroid } from 'react-native'
import { isIOS } from '@UTILS/Common'
import BluetoothStateManager from 'react-native-bluetooth-state-manager'
import { State } from 'react-native-ble-plx'
import DeviceInfo from 'react-native-device-info'

const isEnabledBlu = async () => {
  if (isIOS) return true
  return PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION)
}

const getNeedPermissions = async () => {
  if (isIOS) return []
  const permissions = [PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION]
  if (await DeviceInfo.getApiLevel() >= 31) {
    permissions.push(PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT)
    permissions.push(PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN)
    permissions.push(PermissionsAndroid.PERMISSIONS.BLUETOOTH_ADVERTISE)
  }
  return permissions
}
const isBluetoothOn = async () => {
  const state = await BluetoothStateManager.getState()
  return state === State.PoweredOn
}

const isBleSupportedOnDevice = async () => {
  const state = await BluetoothStateManager.getState()
  return state !== State.Unsupported
}

const hasPermission = async (permissions: Permission[]): Promise<boolean> => {
  if (permissions.length === 0) return true
  for (let i = 0; i < permissions.length; i++) {
    const granted = await PermissionsAndroid.check(permissions[i])
    if (!granted) return false
  }
  return true
}

const hasBluetoothPermission = async () => {
  const permissions = await getNeedPermissions()
  if (permissions.length === 0) return true
  return hasPermission(permissions)
}

const allPrerequisitesOK = async () => {
  const hasPermissions = await hasBluetoothPermission()
  return await isEnabledBlu() && hasPermissions
}

const requestBluPermission = async () => {
  if (isIOS) return true
  BluetoothStateManager.getState()
  const permissions = await getNeedPermissions()
  const granteds = await PermissionsAndroid.requestMultiple(permissions)
  console.log('granteds', granteds, permissions)
  return !Object.values(granteds).find((item) => item !== PermissionsAndroid.RESULTS.GRANTED)
}

const getNeedFilePermissions = async () => {
  if (isIOS) return []
  const permissions = [PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE]
  if (await DeviceInfo.getApiLevel() >= 31) {
    permissions.push(PermissionsAndroid.PERMISSIONS.READ_MEDIA_IMAGES)
  } else {
    permissions.push(PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE)
  }
  return permissions
}

const requestAndroidFilePermission = async () => {
  if (isIOS) return true
  const permissions = await getNeedFilePermissions()
  const hasCheck = await hasPermission(permissions)
  if (hasCheck) return true
  const granters = await PermissionsAndroid.requestMultiple(permissions)
  return !Object.values(granters).find((item) => item !== PermissionsAndroid.RESULTS.GRANTED)
}

export default {
  isEnabledBlu,
  allPrerequisitesOK,
  isBluetoothOn,
  requestBluPermission,
  isBleSupportedOnDevice,
  requestAndroidFilePermission,
}
